﻿Public Class Form1

    Dim full, drive As String
    Dim ck1 = 0
    Dim ck2 = 0
    Dim ck3 = 0
    Dim part
    Dim os = 0
    Dim username As String = Environment.UserName.ToLower
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'prüft ob der bgw noch läuft und beendet diesen dann
        'bedingung - siehe form_load
        If BackgroundWorker1.IsBusy Then
            BackgroundWorker1.CancelAsync()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Computer.Info.OSFullName.Contains("XP") Then
            GroupBox4.BackColor = Color.Yellow
            os = 1
            Label7.Text = My.Computer.Info.OSFullName

        Else
            GroupBox4.BackColor = Color.Gold
            os = 2
            Label7.Text = My.Computer.Info.OSFullName
        End If

        BackgroundWorker1.WorkerSupportsCancellation = True

        If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\OEB", "part", defaultValue:=True) = "" Then
            Button2.Hide()
        Else
            part = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\OEB", "part", defaultValue:=True)
            If Dir(part, vbDirectory) = "" Then
                Button2.Hide()
            Else
                part = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\OEB", "part", defaultValue:=True)
                Label6.Text = part

            End If

        End If
        full = Reflection.Assembly.GetExecutingAssembly.Location
        drive = IO.Path.GetPathRoot(full)
        Label2.Text = username
        Label3.Text = drive


        ' XP
        If os = 1 Then
            If Dir("" & drive & "Dokumente und Einstellungen\" & username & "\Lokale Einstellungen\Anwendungsdaten\Microsoft\Outlook", vbDirectory) = "" Then
            Else
                ck1 = ck1 + 1
                ck2 = 1

            End If
            If Dir("" & drive & "Dokumente und Einstellungen\" & username & "\Anwendungsdaten\Microsoft\Outlook", vbDirectory) = "" Then
            Else
                ck1 = ck1 + 1
                ck3 = 1
            End If
        End If


        'Vista
        If os = 2 Then
            If Dir("" & drive & "Users\" & username & "\Documents\Outlook-Dateien", vbDirectory) = "" Then
            Else
                ck1 = ck1 + 1
                ck2 = 1

            End If
            If Dir("" & drive & "Users\" & username & "\AppData\Local\Microsoft\Outlook", vbDirectory) = "" Then
            Else
                ck1 = ck1 + 1
                ck3 = 1
            End If
        End If


        If ck1 = 0 Then
            Label4.Text = "Bad 0"
            GroupBox3.BackColor = Color.Red
        End If
        If ck1 = 1 Then
            Label4.Text = "OK 1"
            GroupBox3.BackColor = Color.Orange
        End If
        If ck1 = 2 Then
            Label4.Text = "Super 2"
            GroupBox3.BackColor = Color.Green
        End If
    End Sub
    

    Private Sub copy()
        Application.DoEvents()
        Form2.Show()
        Me.Hide()

        
      
        BackgroundWorker1.RunWorkerAsync()

    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            part = FolderBrowserDialog1.SelectedPath
            Dim regKey As Microsoft.Win32.RegistryKey = _
                     Microsoft.Win32.Registry.CurrentUser.CreateSubKey _
                     ("Software\OEB")
            regKey.SetValue("part", part)
            Label6.Text = part

            copy()


        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Text = "Automatic Outlook Backup /  Eclipse Public License   " & My.Computer.Clock.LocalTime

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        copy()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork



        'XP
        If os = 1 Then
            Try
                Dim Time = "" & My.Computer.Clock.LocalTime & ""
                Time = Time.Replace(":", "-")
                MkDir("" & part & "\Outlook1 - " & System.Net.Dns.GetHostName() & " -  " & Time & "")
                MkDir("" & part & "\Outlook2 - " & System.Net.Dns.GetHostName() & " -  " & Time & "")
                If ck2 = 1 Then
                    My.Computer.FileSystem.CopyDirectory("" & drive & "Dokumente und Einstellungen\" & username & "\Lokale Einstellungen\Anwendungsdaten\Microsoft\Outlook", "" & part & "\Outlook1 - " & System.Net.Dns.GetHostName() & " -  " & Time & "", True)
                End If
                If ck3 = 1 Then
                    My.Computer.FileSystem.CopyDirectory("" & drive & "Dokumente und Einstellungen\" & username & "\Anwendungsdaten\Microsoft\Outlook", "" & part & "\Outlook2 - " & System.Net.Dns.GetHostName() & " -  " & Time & "", True)
                End If
            Catch
            End Try
        End If


        'Vista

        If os = 2 Then
            Try
                Dim Time = "" & My.Computer.Clock.LocalTime & ""
                Time = Time.Replace(":", "-")
                MkDir("" & part & "\Outlook1 - " & System.Net.Dns.GetHostName() & " -  " & Time & "")
                MkDir("" & part & "\Outlook2 - " & System.Net.Dns.GetHostName() & " -  " & Time & "")
                If ck2 = 1 Then
                    My.Computer.FileSystem.CopyDirectory("" & drive & "Users\" & username & "\Documents\Outlook-Dateien", "" & part & "\Outlook1 - " & System.Net.Dns.GetHostName() & " -  " & Time & "", True)
                End If
                If ck3 = 1 Then
                    My.Computer.FileSystem.CopyDirectory("" & drive & "Users\" & username & "\AppData\Local\Microsoft\Outlook", "" & part & "\Outlook2 - " & System.Net.Dns.GetHostName() & " -  " & Time & "", True)
                End If
            Catch
            End Try
        End If


    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
     
        Form2.Hide()
        Me.Show()
        MsgBox("" & part & "")
    End Sub
End Class
